package com.ming.reggie.controller;

import com.ming.reggie.common.R;
import com.ming.reggie.dto.DishDto;
import com.ming.reggie.entity.Dish;
import com.ming.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired(required = false)
    HttpSession session;

    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto){
        Long id = (Long) session.getAttribute("employee");

        //设置创建与修改人
        dishDto.setCreateUser(id);
        dishDto.setUpdateUser(id);
        //调用添加的业务方法
        dishService.addDish(dishDto);

        return R.success("添加成功");

    }

    @RequestMapping("/list")
    public R<List<DishDto>> findBycategoryId(Long categoryId,Integer status){
        List<DishDto> dishDtoList = dishService.findByCategoryId(categoryId, status);
        return R.success(dishDtoList);
    }
}
